package boardgamer.yavalath.client.resources;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import boardgamer.yavalath.R;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class StringHandler {

  private static StringHandler INSTANCE = null;

  private final Resources resources;

  public final Uri disclaimerUrl;
  public final Uri proMarketUrl;

  public final String ruleTitle;
  public final String ruleObjectiveHeader;
  public final String ruleObjectiveText;
  public final String ruleGeneralHeader;
  public final String ruleGeneralText1;
  public final String ruleGeneralText2;
  public final String rule3PlayersHeader;
  public final String rule3PlayersText;

  public final String aboutFullName;
  public final String aboutDisclaimerHtml;
  public final String aboutFeedbackHtml;
  public final String aboutReleaseNotesHtml;

  public final String errorInvalidMove;
  public final String errorForcedMove;
  
  public final String saveReplayNoName;
  public final String saveReplayLoadTitle;
  public final String saveReplayDoneFail;
  
  public final String gameDrawMessage;
  public final String gameDrawDialog;

  private StringHandler(Context context) {
    this.resources = context.getResources();

    this.disclaimerUrl = Uri.parse(resources.getString(R.string.disclaimer_url));
    this.proMarketUrl = Uri.parse(resources.getString(R.string.pro_version_market));

    this.ruleTitle = resources.getString(R.string.rule_title);
    this.ruleObjectiveHeader = resources.getString(R.string.rule_objective_header);
    this.ruleObjectiveText = resources.getString(R.string.rule_objective_text);
    this.ruleGeneralHeader = resources.getString(R.string.rule_general_header);
    this.ruleGeneralText1 = resources.getString(R.string.rule_general_text_1);
    this.ruleGeneralText2 = resources.getString(R.string.rule_general_text_2);
    this.rule3PlayersHeader = resources.getString(R.string.rule_3_players_header);
    this.rule3PlayersText = resources.getString(R.string.rule_3_players_text);

    this.aboutFullName = String.format(
        resources.getString(R.string.full_name),
        resources.getString(R.string.app_name),
        resources.getString(R.string.version));
    this.aboutDisclaimerHtml = resources.getString(R.string.disclaimer);
    this.aboutFeedbackHtml = resources.getString(R.string.feedback);
    this.aboutReleaseNotesHtml = String.format(
        resources.getString(R.string.release_note),
        resources.getString(R.string.version));

    this.errorInvalidMove = resources.getString(R.string.error_invalid_move);
    this.errorForcedMove = resources.getString(R.string.error_forced_move);
    
    this.saveReplayNoName = resources.getString(R.string.save_replay_no_name);
    this.saveReplayLoadTitle = resources.getString(R.string.save_replay_load_title);
    this.saveReplayDoneFail = resources.getString(R.string.save_replay_done_fail);
    
    this.gameDrawMessage = resources.getString(R.string.game_draw_message);
    this.gameDrawDialog = resources.getString(R.string.game_draw_dialog);
  }
  
  public String getWinMessage(String winner) {
    return String.format(resources.getString(R.string.game_win_message), winner);
  }
  
  public String getWinMessageDialog(String winner) {
    return String.format(resources.getString(R.string.game_win_message_dialog), winner);
  }
  
  public String getSaveReplayLoadText(String fileName) {
    return String.format(resources.getString(R.string.save_replay_load_text), fileName);
  }
  
  public String getSaveReplayDoneText(String filePath) {
    return String.format(resources.getString(R.string.save_replay_done_text), filePath);
  }

  public static void initialize(Context context) {
    INSTANCE = new StringHandler(context);
  }

  public static StringHandler instance() {
    checkNotNull(INSTANCE, "Cannot get instance before initializing");
    return INSTANCE;
  }
}
